package com.lianrui.commons.validate;

import java.util.Collection;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lianrui.commons.exception.BusinessException;
import com.lianrui.message.BusinessMessageFactory;
import com.lianrui.message.IBusinessMessage;

/**
 * 功能描述：输入对象的校验类
 * 
 * @author LJQ
 * 
 * <br>2016年1月19日 上午11:44:28
 */
public abstract class InputValidate extends AbstractValidate {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(InputValidate.class);
	
	public InputValidate(int mName) {
		super(mName);
	}
	
	/**
	 * 功能说明：对象不能为NULL
	 * @param object 待校验的对象
	 * @param mName 模块名称【消息值】
	 * @return
	 * @throws BusinessException
	 */
	public InputValidate noNull(Object object) throws BusinessException {
		if (object == null)
		throw new BusinessException(IBusinessMessage.VALID_INPUT_NONULL, LOGGER,
				BusinessMessageFactory.formatMessage(mName));
		return this;
	}
	
	/**
	 * 功能说明：对象不能为NULL
	 * @param object 待校验的对象
	 * @param mName 模块名称【消息值】
	 * @param fName 字段域名称【消息值】
	 * @return
	 * @throws BusinessException
	 */
	public InputValidate noNull(Object object, int fName) throws BusinessException {
		if (object == null)
			throw new BusinessException(IBusinessMessage.VALID_INPUT_FIELDNONULL, LOGGER,
					BusinessMessageFactory.formatMessage(mName),
					BusinessMessageFactory.formatMessage(fName));
		return this;
	}
	
	/**
	 * 功能说明：集合不能为空
	 * @param collection 待校验的集合
	 * @param mName 模块名称【消息值】
	 * @param fName 字段域名称【消息值】
	 * @return
	 * @throws BusinessException
	 */
	public InputValidate noEmpty(Collection<?> collection, int fName) throws BusinessException {
		if (CollectionUtils.isEmpty(collection))
		throw new BusinessException(IBusinessMessage.VALID_INPUT_NOEMPTY, LOGGER,
				BusinessMessageFactory.formatMessage(mName),
				BusinessMessageFactory.formatMessage(fName));
		return this;
	}
	
	/**
	 * 功能说明：数据不能为空，包括：null|[]
	 * @param array 待校验的数组
	 * @param mName 模块名称【消息值】
	 * @param fName 字段域名称【消息值】
	 * @return
	 * @throws BusinessException
	 */
	public InputValidate noEmpty(Object[] array, int fName) throws BusinessException {
		if (ArrayUtils.isEmpty(array))
			throw new BusinessException(IBusinessMessage.VALID_INPUT_NOEMPTY, LOGGER,
					BusinessMessageFactory.formatMessage(mName),
					BusinessMessageFactory.formatMessage(fName));
		return this;
	}
	
	/**
	 * 功能说明：字符串不能为空，包括：null|""|" "
	 * @param value 待校验的字符串
	 * @param mName 模块名称【消息值】
	 * @param fName 字段域名称【消息值】
	 * @return
	 * @throws BusinessException
	 */
	public InputValidate noEmpty(String value, int fName) throws BusinessException {
		if (StringUtils.isBlank(value))
			throw new BusinessException(IBusinessMessage.VALID_INPUT_NOEMPTY, LOGGER,
					BusinessMessageFactory.formatMessage(mName),
					BusinessMessageFactory.formatMessage(fName));
		return this;
	}
	
	/**
	 * 功能说明：最终验证方法
	 * @return
	 */
	public int validate() {
		return 0;
	}
}
